/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.client.mapviewer;

import flash.display.Sprite;
import flash.geom.Rectangle;
import com.chasekernan.hxnova.utils.Vector;
import com.chasekernan.hxnova.utils.Utils;
import com.chasekernan.hxnova.core.stars.Star;
import com.chasekernan.hxnova.core.SpaceObject;
import com.chasekernan.hxnova.core.players.Player;
import com.chasekernan.hxnova.core.players.PlayerRelations;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class StarObject extends Sprite, implements IMapObject {
    
    public static var MAX_GREEN_STAR = 0x00FF00;
    public static var MIN_GREEN_STAR = 0x787a50;
    public static var MAX_RED_STAR = 0xff0000;
    public static var MIN_RED_STAR = 0xff8000;
    
    public static var UNKNOWN_STAR = 0x999999;
    
    public static var MAX_RADIUS = 5;
    public static var MIN_POP_RADIUS = 2;
    public static var HW_RADIUS = 6;
    public static var MIN_RADIUS = 1;
    public static var LINE_THICKNESS = 1.25;
    public static var GREEN_ADDED_THICKNESS = 0.25;
    
    public static var FLAG_LINE = 0.25;

    public var star     : Star;
    public var color    : Int;
    private var nameBox : TextField;
    
    public function new(star : Star) {
        super();
        
        this.star = star;
        nameBox = Utils.createGenericTextBox(star.name);
        addChild(nameBox);
    }
    
    public function getObject() : SpaceObject {
        return star;
    }
    
    public function draw(bounds : Rectangle, player : Player, 
                         options : MapOptions) {
        graphics.clear();
        var loc = star.location;
        x = loc.x - bounds.x;
        y = loc.y - bounds.y;
        
        if (star.concentrations == null || star.environment == null) {
            graphics.lineStyle(LINE_THICKNESS, UNKNOWN_STAR);
            graphics.drawCircle(0, 0, MIN_RADIUS);
        } else {
            var habPercent = star.getHabPercent(player.race);
            
            color = 
                if (habPercent < 0) 
                    Utils.mixColors(MIN_RED_STAR, MAX_RED_STAR, habPercent / -0.45)
                else
                    Utils.mixColors(MIN_GREEN_STAR, MAX_GREEN_STAR, habPercent);
                    
            var thickness = LINE_THICKNESS;
            
            graphics.lineStyle(thickness, color);
            
            var size = 
                if (star.isHomeworld != null && star.isHomeworld)
                    HW_RADIUS
                else if (star.population == null || star.population == 0) 
                    MIN_RADIUS
                else
                    Math.max(MIN_POP_RADIUS, 
                            MAX_RADIUS * (star.population / Star.MAX_POPULATION));
            graphics.drawCircle(0, 0, size);
            
            graphics.lineStyle(0.25, color);
            graphics.drawCircle(0, 0, 0.1);
            
            if (options.drawStarNames) {
                nameBox.x = - 50 / options.zoom;
                nameBox.y = size;
                nameBox.scaleX = 1 / options.zoom;
                nameBox.scaleY = 1 / options.zoom;
                nameBox.visible = true;
            } else {
                nameBox.visible = false;
            }
            
            
            if (options.drawStarOwner) {
                if (star.owner != null && star.owner.id != null && 
                        star.owner.id != -1) {
                            
                    if (star.owner.id == player.id) {
                        drawSelfFlag();
                    } else if (player.relations != null) {
                        drawOtherFlag(
                            player.relations.getRelationTo(star.owner));
                    }
                }
            }
        }
    }
    
    private inline function drawFlag(color : Int) {
        graphics.lineStyle(FLAG_LINE, color);
        graphics.moveTo(0, 0);
        graphics.lineTo(0, -13);
        
        graphics.drawRect(1.5, -13, 10, 5);
    }
    
    private inline function drawOtherFlag(relation : Relation) {
        drawFlag(switch (relation) {
            case Relation.Friend: MapViewer.FRIENDLY_COLOR;
            case Relation.Nuetral: MapViewer.NUETRAL_COLOR;
            case Relation.Enemy: MapViewer.ENEMY_COLOR;
        });
    }
    
    private inline function drawSelfFlag() {
        drawFlag(MapViewer.SELF_COLOR);
    }
    
}